package yxy.game.pm2.module.resonate;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.message.ResonateMessage;
import yxy.game.pm2.module.hero.HeroHandle;

import java.util.*;


//共鸣系统
public class ResonateModule extends PlayerModule {

    public final static int resonateHeroQuality = 20;  //共鸣解锁需要20级或以上资质

    public final static int resonateHeroCDMinute = 12 * 60;  //共鸣的cd，单位分钟

    // bean===========================================================================
    private HashMap<String, ResonateHeroMsg> aboveHeros = new HashMap<String, ResonateHeroMsg>();//场上的武将  key：position

    private ArrayList<ResonateHeroMsg> resonateHeros = new ArrayList<ResonateHeroMsg>();//共鸣的武将

    private Boolean isOpen; //系统是否启动

    private HashMap<String, DateTime> updateTime = new HashMap<String, DateTime>();//共鸣武将更新时间 key : 格子位置

    public HashMap<String, ResonateHeroMsg> getAboveHeros() {
        return aboveHeros;
    }

    public void setAboveHeros(HashMap<String, ResonateHeroMsg> aboveHeros) {
        this.aboveHeros = aboveHeros;
    }

    public ArrayList<ResonateHeroMsg> getResonateHeros() {
        return resonateHeros;
    }

    public void setResonateHeros(ArrayList<ResonateHeroMsg> resonateHeros) {
        this.resonateHeros = resonateHeros;
    }


    public HashMap<String, DateTime> getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(HashMap<String, DateTime> updateTime) {
        this.updateTime = updateTime;
    }

    public Boolean getOpen() {
        return isOpen;
    }

    public void setOpen(Boolean open) {
        isOpen = open;
    }

    // func===========================================================================

    public boolean cooldown(int field) {
        if (getUpdateTime() == null || getUpdateTime().size() == 0 || getUpdateTime().get(String.valueOf(field))==null) {
            return false;
        }

        DateTime dateTime = getUpdateTime().get(String.valueOf(field));
        return DateTime.now().isBefore(dateTime);
    }


    // redis==========================================================================

    private static String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:RESONATE", playerCode);
    }


    public void set() {
        try {
            setv(key(getPlayerCode()), this);
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    static public ResonateModule get(String playerCode) {

        try {
            ResonateModule module = getv(key(playerCode));

            if (module == null) {
                module = MySQLCacheDAO.getV(key(playerCode), ResonateModule.class);
                if (module != null) {
                    module.set();
                }//把数据同步到redis
            }

            //todo 后期放数据库
            if (module == null) {
                module = create(playerCode);
            }
            module.check();
            return module;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //红点
    private void check() {

    }


    static public ResonateModule create(String playerCode) {
        ResonateModule resonateModule = new ResonateModule();
        //自动存前6个等级最高的武将进共鸣里面
        HeroHandle heroHandle = new HeroHandle();
        Collection<Hero> heros = heroHandle.getHeros(playerCode);
        if (heros.size() < 6) {
            resonateModule.setUpdateTime(new HashMap<>());
            resonateModule.setPlayerCode(playerCode);
            resonateModule.setOpen(false);
            resonateModule.set();
            return resonateModule;
        }


        Iterator<Hero> iterator = heros.iterator();
        int num = 0;
        while (iterator.hasNext()) {
            num++;
            if (num > 6) break;
            ResonateHeroMsg heroMsg = new ResonateHeroMsg();
            Hero hero = iterator.next();
            heroMsg.setLevel(hero.getLevel());
            heroMsg.setStage(hero.getStage());
            heroMsg.setStar(hero.getStar());
            heroMsg.setPosition(getPositionFromIndex(num));
            heroMsg.setCode(hero.getCode());
            heroMsg.setHeroId(hero.getDataId());
            resonateModule.getAboveHeros().put(getPositionFromIndex(num), heroMsg);

        }
        resonateModule.setUpdateTime(new HashMap<>());
        resonateModule.setPlayerCode(playerCode);
        resonateModule.setOpen(true);
        resonateModule.set();
        return resonateModule;
    }


    public static String getPositionFromIndex(int index){
        switch (index) {
            case 1:
                return "FU";
            case 2:
                return "FM";
            case 3:
                return "FD";
            case 4:
                return "BU";
            case 5:
                return "BM";
            case 6:
                return "BD";
            default:
                return null;
        }
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                ResonateModule resonateModule = getv(key(playerCode));
                if (resonateModule != null) MySQLCacheDAO.setV(key, resonateModule);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    // lock==========================================================================

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }

}
